package com.lzx.applib.base;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import java.lang.ref.WeakReference;

/**
 * Created by Lzx on 16/10/20 0020.
 */

public class SafeHandler extends Handler {

    WeakReference<Activity> activityWeakReference;

    public SafeHandler(Activity activity) {
        activityWeakReference = new WeakReference<Activity>(activity);
    }

    public SafeHandler(Activity activity, Callback callback) {
        super(callback);
        activityWeakReference = new WeakReference<Activity>(activity);
    }

    @Override
    public final void dispatchMessage(Message msg) {
        if (activityWeakReference.get()!=null && !activityWeakReference.get().isFinishing()) {
            super.dispatchMessage(msg);
        }
    }

}
